package tld.test.interpreter.language
{
	import tld.test.interpreter.Context;
	import tld.test.interpreter.IContext;
	
	public class LanguageContext implements IContext
	{
		private var _parent:IContext;
		
		private const _names:Object = 
		{
			"set" : set,
			"throw" : throwError
		};
		
		private const _reserved:Array = ["set", "print", "throw"];
		
		public function LanguageContext(parent:IContext)
		{
			super();
			this._parent = parent;
		}
		
		public function getParent():IContext
		{
			return this._parent;
		}
		
		public function setParent(parent:IContext):IContext
		{
			if (this._parent) parent.setParent(this._parent);
			return this._parent = parent;
		}
		
		public function getNames():Object
		{
			var result:Object;
			if (this._parent)
				result = Context.merge(
					this._names, this._parent.getNames());
			else result = this._names;
			return result;
		}
		
		public function put(name:String, value:Object = null):void
		{
			this._names[name] = value;
		}
		
		public function resolve(name:String):Object
		{
			return this._names[name];
		}
		
		private function set(name:String, value:Object):Object
		{
			if (!(name in this._reserved))
				this._names[name] = value;
			else throw "Cannot set: " + name;
			return value;
		}
		
		private function throwError(error:Object):void
		{
			throw error;
		}
	}
}